package com.jxwifi.cloud.quickclean.toolkit;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.address.AddressListActivity;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.ClientBasicActivity;
import com.jxwifi.cloud.quickclean.app.PasswordLogin;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.bean.AddPreOrderResVoBean;
import com.jxwifi.cloud.quickclean.bean.HomeaddressBean;
import com.jxwifi.cloud.quickclean.bean.ShopPurchaseDtoBean;
import com.jxwifi.cloud.quickclean.order.AddRemarksActivity;
import com.jxwifi.cloud.quickclean.orderdetails.OrderDetailsActivity;
import com.jxwifi.cloud.quickclean.pay.PayResult;
import com.jxwifi.cloud.quickclean.utils.TitleClient;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.umeng.analytics.MobclickAgent;

import org.simple.eventbus.Subscriber;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TKConfirmActivity extends ClientBasicActivity implements TKConfirmView {

    @Bind(R.id.et_tk_confirm_name)
    EditText mEtTkConfirmName; //联系人
    @Bind(R.id.et_tk_confirm_phone)
    EditText mEtTkConfirmPhone;//电话
    @Bind(R.id.tv_tk_address)
    TextView mTvTkAddress;//地址
    @Bind(R.id.tv_tk_confirm_address_details)
    TextView mTvTkConfirmAddressDetails;
    @Bind(R.id.tv_tk_confirm_modify)
    TextView mTvTkConfirmModify;
    @Bind(R.id.tv_tk_confirm_price)
    TextView mTvTkConfirmPrice;//工具包价格
    @Bind(R.id.rv_tk_confirm_list)
    RecyclerView mRvTkConfirmList;
    @Bind(R.id.tv_tk_confirm_remarks_text)
    TextView mTvTkConfirmRemarksText;
    @Bind(R.id.tv_tk_confirm_all_price)
    TextView mTvTkConfirmAllPrice;

    private TKConfirmPresenter mPresenter;

    private ShopPurchaseDtoBean mShopPurchase;
    private String mAddressType;
    private String mWXPayCode;//微信支付接口返回的code

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tool_kit_confirm);
        ButterKnife.bind(this);
        openEventBus();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        initHead();
        mShopPurchase = (ShopPurchaseDtoBean) getIntent().getExtras().getSerializable("shoppur");
        mPresenter=new TKConfirmPresenter(mBaseContext,this);
        mPresenter.initView();
    }

    private void initHead() {
        TitleClient holder = new TitleClient(ButterKnife.findById(this, R.id.lin_client_title));
        holder.defineTitle("确认订单");
        holder.defineLeft(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                onBackPressed();
            }
        });
    }

    @OnClick({R.id.bt_tk_confirm_button, R.id.tv_tk_confirm_modify,R.id.rel_con_order_remarks})
    void clickCK(View v) {
        Intent intent=new Intent();
        switch (v.getId()) {
            //确定订单
            case R.id.bt_tk_confirm_button:
                mPresenter.paySetMeal();
                MobclickAgent.onEvent(mBaseContext, "bt_tk_confirm_button");
                break;
            //修改地址
            case R.id.tv_tk_confirm_modify:
                intent.setClass(mBaseContext, AddressListActivity.class);
                startActivity(intent);
                MobclickAgent.onEvent(mBaseContext, "tv_tk_confirm_modify");
                break;
            case R.id.rel_con_order_remarks:
                intent.setClass(mBaseContext, AddRemarksActivity.class);
                startActivity(intent);
                MobclickAgent.onEvent(mBaseContext, "rel_con_tk_remarks");
                break;
        }
    }

    @Subscriber(tag = "e-addremarks")
    private void getRemarks(String remarks) {
        mTvTkConfirmRemarksText.setText(remarks);
    }

    @Override
    public void onAddressBean(HomeaddressBean address) {
        if (address != null) {
            mTvTkAddress.setText(address.getProvince() + " " + address.getCity() + " " + address.getArea());
            mTvTkConfirmAddressDetails.setText(address.getAddress());
            mAddressType=StateUtils.MODIFY; //修改地址
            mTvTkConfirmModify.setText("修改地址");
        }else{
            mAddressType=StateUtils.ADD_TO;//添加地址
            mTvTkConfirmModify.setText("添加地址");
        }
    }

    @Subscriber(tag = "e-PayResult")
    private void getCondItion(PayResult mBean) {
        Toast toast = Toast.makeText(mBaseContext, "支付成功", Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
        if (!TextUtils.isEmpty(mBean.orderCode)) {
            Intent intent = new Intent();
            intent.setClass(mBaseContext, TKDetailsActivity.class);
            intent.putExtra("tk_code", mBean.orderCode);
            startActivity(intent);
            finish();
        }
    }

    @Subscriber(tag = "e-WXPayResult")
    private void getWXCondItion(PayResult mBean) {
        Toast toast = Toast.makeText(mBaseContext, "支付成功", Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
        if (!TextUtils.isEmpty(mWXPayCode)) {
            Intent intent = new Intent();
            intent.setClass(mBaseContext, TKDetailsActivity.class);
            intent.putExtra("tk_code", mWXPayCode);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void oninfo() {
        PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin) {
            mPresenter.getinfo();
        }
    }

    @Override
    public void getWXPayCode(String mOutTradNo) {
        mWXPayCode=mOutTradNo;
    }

    @Override
    public ShopPurchaseDtoBean getShopPur() {
        return mShopPurchase;
    }

    @Override
    public void onFailure(int errorCode, String msg) {
        Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
    }

    @Override
    public TextView getAllPrice() {
        return mTvTkConfirmAllPrice;
    }

    @Override
    public TextView getRemarksText() {
        return mTvTkConfirmRemarksText;
    }

    @Override
    public RecyclerView getRvView() {
        return mRvTkConfirmList;
    }

    @Override
    public TextView getTkConfirmPrice() {
        return mTvTkConfirmPrice;
    }

    @Override
    public TextView getConfirmModify() {
        return mTvTkConfirmModify;
    }

    @Override
    public TextView getAddressDetails() {
        return mTvTkConfirmAddressDetails;
    }

    @Override
    public TextView getTkAddress() {
        return mTvTkAddress;
    }

    @Override
    public EditText getConfirmName() {
        return mEtTkConfirmName;
    }

    @Override
    public EditText getConfirmPhone() {
        return mEtTkConfirmPhone;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.initView();
        mPresenter.getinfo();

    }
}
